﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using eTMS.Controllers;
using eTMS.Controllers.LopService;

namespace eTMS.Forms.Students
{
    public partial class FrmChuyenNganhHoc : Form
    {
        public FrmChuyenNganhHoc()
        {
            InitializeComponent();
        }

        private SinhVienController controller = new SinhVienController();

        public void SetDataRows(List<DataRow> list)
        {
            if (list.Count > 0)
            {
                controller.Table = list[0].Table.Clone();
                foreach (DataRow row in list)
                {
                    controller.Table.ImportRow(row);
                }
                int NganhHoc_ID = controller.ConvertInt(list[0]["NganhHoc_ID"]);
                int Lop_ID = controller.ConvertInt(list[0]["Lop_ID"]);
                chonNganhHoc2.SetDefaultNganhHoc(NganhHoc_ID);
                comBoBoxLop.SelectedValue = Lop_ID;
                sinhVienList1.SetController(controller);
                sinhVienList1.UpdateToScreen();
            }
        }
        public int KhoaHoc_ID
        {
            set
            {
                KhoaController.LoadAllKhoa(false);
                comboBoxKhoaHoc1.BindData();
                comboBoxKhoaHoc1.SelectedValue = value;
            }
        }


        private void BindLop(int NganhHoc_ID)
        {
            LopController lopController = new LopController();
            List<LopEntity> ls = lopController.LoadAllLopByNganhHoc_IDKhoaHoc_ID(NganhHoc_ID, (int)comboBoxKhoaHoc1.SelectedValue);
            comBoBoxLop.DataSource = ls;
            comBoBoxLop.DisplayMember = "MaLop";
            comBoBoxLop.ValueMember = "Lop_ID";
        }

        private void chonNganhHoc2_OnChangedSelection(object sender, EventArgs e)
        {
            if (chonNganhHoc2.GetSelectedNganhHoc_ID() < 0 || comboBoxKhoaHoc1.SelectedIndex < 0) return;
            BindLop(chonNganhHoc2.GetSelectedNganhHoc_ID());
        }

        private void ucCommandDialog1_OnOK(object sender, CancelEventArgs e)
        {
            foreach (DataRow row in controller.Table.Rows)
            {
                controller.SavePhanNganh(controller.ConvertInt(row["SinhVien_ID"]), (int)comboBoxKhoaHoc1.SelectedIndex, chonNganhHoc2.GetSelectedNganhHoc_ID(), controller.ConvertInt(row["PhanNganhSinhVien_ID"]));
            }
            foreach (DataRow row in controller.Table.Rows)
            {
                row["Lop_ID"] = (int)comBoBoxLop.SelectedValue;
            }
            controller.PhanLopChoSinhVien();
        }

        private void FrmChuyenNganhHoc_Load(object sender, EventArgs e)
        {

        }
    }
}
